﻿//PlazaTableSerializer.cs
//Copyright (c) 2013 StockSharp LLC, all rights reserved.
//This code module is part of StockSharp library.
//This code is licensed under the GNU GENERAL PUBLIC LICENSE Version 3.
//See the file License.txt for the license details.
//More info on: http://stocksharp.com

namespace StockSharp.Plaza.Metadata
{
	using System;
	using System.Collections.Generic;
	using System.IO;
	using System.Text;

	using Ecng.Common;

	class PlazaTableSerializer
	{
		public void Serialize(PlazaStream stream, IEnumerable<PlazaTable> tables)
		{
			if (stream == null)
				throw new ArgumentNullException("stream");

			var buffer = new StringBuilder();

			//buffer.AppendLine("; ----------------------------------------------------------------------------");
			//buffer.AppendLine("; {0}".Put(table.Name));

			buffer.AppendLine("[dbscheme:{0}]".Put(stream.ReplicationSchema));

			foreach (var table in tables)
				buffer.AppendLine("table={0}".Put(table.Name));

			foreach (var table in tables)
			{
				buffer.AppendLine();
				buffer.AppendLine("[table:{0}:{1}]".Put(table.Stream.ReplicationSchema, table.Name));

				foreach (var column in table.Columns)
				{
					buffer.AppendLine("field={0},{1}".Put(column.Name, column.PlazaType));
				}

				// TODO: Для базового клиента еще нужно указать индексы, для безбазового они не нужны.
			}

			File.WriteAllText(stream.ConfigFileName, buffer.ToString());
		}
	}
}